<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

</body>
<script>
  const arr = [20, 66, 15, 2]
  console.log(arr.sort());

  console.log(arr.sort((a, b) => {
    // 转为数字型
    a = +a
    b = +b
    // 升序
    return a - b > 0 ? 1 : -1
    // 降序
    // return a - b > 0 ? -1 : 1
  }));

  console.log(arr.sort((a, b) => {
    // 升序
    return a - b
  }));
  console.log(arr.sort((a, b) => {
    // 降序
    return b - a
  }));
</script>

</html>